package games.charly
{
	import games.BaseGame;
	import org.flixel.FlxButton;
	import org.flixel.FlxCamera;
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxObject;
	import org.flixel.FlxPoint;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxRect;
	import org.flixel.FlxText;
	
	public class CharlyGame extends BaseGame
	{
		[Embed(source="../../../assets/charly/Techo.png")] private var imgTecho:Class;		// esto es una paja ! pensá en processing todo lo que tenés que hacer para conseguir lo mismo.
		[Embed(source="../../../assets/charly/BaseHotel.png")] private var imgBaseHotel:Class;	// te va escribir en castellano?
		[Embed(source="../../../assets/charly/PisoHotel.png")] private var imgPisoHotel:Class;  // no, pero como las imágenes de los chicos estaban en castellano, lo deje asi
		[Embed(source="../../../assets/charly/Pileta.png")] private var imgPileta:Class;
		[Embed(source="../../../assets/charly/Nube1.png")] private var imgNube1:Class;
		[Embed(source="../../../assets/charly/Nube2.png")] private var imgNube2:Class;
		[Embed(source="../../../assets/charly/Nube3.png")] private var imgNube3:Class;
		
		private var charly:CharlyChar; //Char es de Character pero no se si me gusta del todo..
		private var newBuildingBlockPos:int;
		private var blockHeight:int;
		private var pileta:FlxSprite;
		private var pasto:FlxSprite;
		protected var blocks:FlxGroup;
		protected var balcones:FlxGroup;
		protected var obstacles:FlxGroup;
		protected var energies:FlxGroup;
		protected var clouds:FlxGroup;
		
		override public function create():void
		{
			super.create();
			
			FlxG.bgColor = 0xff6ec6f2;
			
			var techo:FlxSprite = new FlxSprite(FlxG.width, 140, imgTecho); 
			techo.x -= techo.width;
			// aca empieza el quilombo
			// que son esos numeros !!! nunca hagas eso ! ponele un nombre
			// pasa que todos esos numeros son los anchos y altos de las imagenes que no puedo acceder de otra forma
			add(techo);
			
			obstacles = new FlxGroup();
			energies = new FlxGroup();
			clouds = new FlxGroup();
			balcones = new FlxGroup();
			
			add(clouds);			
			newBuildingBlockPos = techo.y + techo.height;
			blockHeight = 134;
			
			while (newBuildingBlockPos < techo.y + techo.height + blockHeight * 40)
			{
				createCloud(newBuildingBlockPos + Math.random() * 180 - 90);
				createCloud(newBuildingBlockPos + Math.random() * 180 - 90);
				
				// parametriza todo esto en un metodo
				// para luego hacer creaciones de niveles mas sofisticadas
				createObstacle(Math.random() * ((FlxG.width - 206) / 3), newBuildingBlockPos + Math.random() * blockHeight);
				createObstacle(Math.random() * ((FlxG.width - 206) / 3) + (FlxG.width - 206) / 3, newBuildingBlockPos + Math.random() * blockHeight);
				createObstacle(Math.random() * ((FlxG.width - 206) / 3) + ((FlxG.width - 206) / 3) * 2, newBuildingBlockPos + Math.random() * blockHeight);
				
				createEnergy(newBuildingBlockPos + Math.random() * 180 - 90);
				
				createBuildingBlock(newBuildingBlockPos);
				
				newBuildingBlockPos += blockHeight;
			}
			
			var baseHotel:FlxSprite = new FlxSprite(FlxG.width, newBuildingBlockPos, imgBaseHotel);
			baseHotel.x -= baseHotel.width;
			var pisoHotel:FlxSprite = new FlxSprite(FlxG.width, baseHotel.y + baseHotel.height - 26, imgPisoHotel);
			pisoHotel.x -= 238;
			
			pileta = new FlxSprite(-60, pisoHotel.y - 20, imgPileta);
			pasto = new FlxSprite(0, pileta.y - 20);			
			pasto.makeGraphic(FlxG.width, pileta.height + 60, 0xff61dc79);
			pasto.width = FlxG.width;
			pasto.height = pileta.height + 60;
			
			add(pasto);
			
			//blocks = new FlxGroup();
			//blocks.add(pasto);
			//add(blocks);
			
			add(pisoHotel);
			add(baseHotel);
			add(pileta);
			
			add(obstacles);
			add(energies);
			add(balcones);
			
			charly = new CharlyChar(techo.x, techo.y, pasto.y, pileta.x + pileta.width - 60);
			add(charly);
			
			FlxG.worldBounds = new FlxRect(0, 0, FlxG.width, pasto.y + pasto.height);
			
			FlxG.camera.follow(charly);  // ahah !!! boludo ! que groso esto !!!
			FlxG.camera.deadzone = new FlxRect(0, 100, FlxG.width, 50);
			FlxG.camera.bounds = new FlxRect(0, 0, FlxG.width, pasto.y + pasto.height);  // despues me explicas bien como funciona la camara
		}
		
		override public function update():void
		{
			super.update();
			
			FlxG.overlap(charly, obstacles, collidedObstacle);
			FlxG.overlap(charly, energies, collidedEnergy);
			//charly.balcon = false;
			FlxG.collide(charly, balcones);
			//trace(charly.balcon);
		}
		
		private function createBuildingBlock(_y:int):void
		{
			var b:BuildingBlock = new BuildingBlock(FlxG.width, _y);
			add(b);
			
			var colisionBalcon:FlxSprite = new FlxSprite(b.x, b.y+50);
			colisionBalcon.makeGraphic(40, 10, 0xffff0000);
			colisionBalcon.alpha = 0.0;
			colisionBalcon.width = 40;
			colisionBalcon.height = 10;
			colisionBalcon.allowCollisions = FlxObject.UP;
			colisionBalcon.immovable = true;
			balcones.add(colisionBalcon);
		}
		
		private function createCloud(_y:int):void
		{
			var n:FlxSprite
			if (Math.random() < 0.333)
			{
				n = new FlxSprite(Math.random() * (FlxG.width - 206), _y, imgNube1);
			}
			else if (Math.random() < 0.5)
			{
				n = new FlxSprite(Math.random() * (FlxG.width - 206), _y, imgNube2);
			}
			else
			{
				n = new FlxSprite(Math.random() * (FlxG.width - 206), _y, imgNube3);
			}
			
			if (Math.random() < 0.5)
			{
				n.scale = new FlxPoint( -1, 1);
			}
			
			clouds.add(n);
		}
		
		private function createObstacle(_x:Number, _y:Number):void
		{
			var o:Obstacle = new Obstacle(_x, _y);
			obstacles.add(o);
		}
		
		private function createEnergy(_y:int):void
		{
			var e:Energy = new Energy(Math.random() * (FlxG.width - 206), _y);
			energies.add(e);
		}
		
		public function collidedObstacle(obj1:FlxObject,obj2:FlxObject):void
		{
			charly.hurt(1);
			obj2.kill();
		}
		
		public function collidedEnergy(obj1:FlxObject,obj2:FlxObject):void
		{
			charly.recover(1);
			obj2.kill();
		}
		
		//public function collidedBalcon(obj1:FlxObject,obj2:FlxObject):void
		//{
			//charly.recover(1);
			//obj2.kill();
			//charly.balcon = true;
		//}
		
		override protected function onReplay():void 
		{
			FlxG.switchState(new CharlyGame());
		}
	}
}